package 第十一章_网络编程.第十一章_网络编程_MINA;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.io.IOException;
import java.net.InetSocketAddress;

public class Server {
    public static void main(String[] args) {
        //创建一个非阻塞的Server端Socket NIO
        SocketAcceptor acceptor=new NioSocketAcceptor();
        DefaultIoFilterChainBuilder chain=acceptor.getFilterChain();
        //设定一个过滤器，一行一行的读取数据(/r/n).
        //1. 是文本传输，2.是对象传输
        //chain.addLast("myChain",new ProtocolCodecFilter(new TextLineCodecFactory()));
        chain.addLast("objectfilter",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        //设置服务器端的消息处理器
        acceptor.setHandler(new MinaServerHandler());
        int port =9999;

        try {
            //这边绑定需要全部完成的接口
            acceptor.bind(new InetSocketAddress("localhost",9999));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Mina Server running, listener on: "+port);

    }
}
